package com.redis.demo.controller;

import com.redis.demo.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author zqf
 * @Date 2022/10/27 14:55
 * @Description: Hash的使用
 */
@RestController("/hashKey")
@Api(tags = "redis对哈希的操作")
public class HashRedisController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("add")
    @ApiOperation("添加map并返回")
    public Map testHsetHash(@RequestParam("redisKey") String redisKey) {
        redisTemplate.opsForHash().put(redisKey, "name", "张三");
        redisTemplate.opsForHash().put(redisKey, "email", "abcd@163.com");
        return redisTemplate.opsForHash().entries("myHash");
    }

    @GetMapping("addmore")
    @ApiOperation("在hash表中一次性插入多个元素")
    public Map<Object, Object> testHMsetHash(@RequestParam("redisKey") String redisKey) {
        Map<String, String> map = new HashMap<>();
        map.put("name", "lisi");
        map.put("email", "xl_echo@163.com");
        map.put("blogName", "xlecho");
        map.put("address", "贵阳市南明区");
        redisTemplate.opsForHash().putAll(redisKey, map);
        return redisTemplate.opsForHash().entries(redisKey);
    }


    @GetMapping("getListKey")
    @ApiOperation("在hash表中批量获取key对应的值")
    public List testHMgetHash(@RequestParam("redisKey") String redisKey) {
        List<Object> list = new ArrayList<>();
        list.add("name");
        list.add("email");
        list.add("blogName");
        list.add("address");
        return redisTemplate.opsForHash().multiGet(redisKey, list);
    }


    @GetMapping("getAllHKey")
    @ApiOperation(" 获取hash表中的所有hkey")
    public Set testHkeysHash(@RequestParam("redisKey") String redisKey) {
        return redisTemplate.opsForHash().keys(redisKey);
    }


    @GetMapping("getAllHValue")
    @ApiOperation("获取hash表中的所有hvalue")
    public List testHvalsHash(@RequestParam("redisKey") String redisKey) {
        return redisTemplate.opsForHash().values(redisKey);
    }


    @GetMapping("hasHKey")
    @ApiOperation("判断hash表中是否存在这个key")
    public Boolean testHexistsHash(@RequestParam("redisKey") String redisKey, @RequestParam("hashKey") String hashKey) {
        return redisTemplate.opsForHash().hasKey(redisKey, hashKey);

    }


    @GetMapping("delHKey")
    @ApiOperation("删除myHash中的某一个键值对")
    public Map testHdelHash(@RequestParam("redisKey") String redisKey, @RequestParam("hashKey") String hashKey) {
        redisTemplate.opsForHash().delete(redisKey, hashKey);
        return redisTemplate.opsForHash().entries(redisKey);
    }


    @GetMapping("redisKeySize")
    @ApiOperation("获取myHash的长度")
    public Long testHlenHash(@RequestParam("redisKey") String redisKey) {
        return redisTemplate.opsForHash().size(redisKey);
    }
}
